clear
set more off
**Aaron Flaaen
**August 11, 2017
**Last Updated:

********************************************************************************
/* SUMMARY OF THIS FILE
FILES THAT MUST BE RUN FIRST: 


	
OUTPUT: analysisdata_manuf.dta						      */
********************************************************************************




global OTHER $dir
global DATA  $dir
global BFPNDATA  $dir

cd $DATA

**--------------------------------------------------------------------------
**Step 1: Create Industry for Firm (From LBD)
**--------------------------------------------------------------------------

**Need to Attach Firm Industry to our LBD-DCA dataset. I propose we use same method as in old project

forvalues yr=2009(1)2012 {
 
         !gunzip $BFPNDATA/lbd`yr'c.dta.gz
 
         use $BFPNDATA/lbd`yr'c.dta, clear
	 
	  **For single-units, replace firmid==cfn (for lfttd)
         replace firmid=cfn if mu==0
	 
         keep firmid emp bestnaics
         drop if firmid==""
 
 
         **Naics Variables
         gen naics2 = substr(bestnaics,1,2)
         gen naics3 = substr(bestnaics,1,3)
         gen naics4 = substr(bestnaics,1,4)
         gen naics5 = substr(bestnaics,1,5)
         gen naics6 = substr(bestnaics,1,6)
         gen naics7 = substr(bestnaics,1,7)
         gen naics8 = substr(bestnaics,1,8)
 
 
         **Determine Firm Industry
         bys firmid naics2: egen sumemp = sum(emp)
         bys firmid: egen maxemp = max(sumemp)
         gen naics2max = naics2 if maxemp==sumemp
         drop sumemp maxemp
         forvalues i = 3(1)8 {
                 local j = `i'-1
                 bys firmid naics`i': egen sumemp = sum(emp) if naics`j'max~=""
                 bys firmid: egen maxemp = max(sumemp)
                 gen naics`i'max = naics`i' if maxemp==sumemp
                 drop sumemp maxemp
         }
 
         drop naics2max naics3max naics4max naics5max naics6max naics7max
         rename naics8max naics_code
 
         collapse (firstnm) naics_code, by(firmid)
         sort firmid
         save firmnaics`yr'.dta, replace
	 !gzip $BFPNDATA/lbd`yr'c.dta
 
 }
 
**--------------------------------------------------------------------------
**Step 2: Bring in DCA_LBD_ Data with Country Identifiers
**--------------------------------------------------------------------------
 
**We will be using the lbd_dca_`i'_clean.dta datasets for LBD-MULT-Country-Parent Info
** See lbd_dca_prep.do for details (modified from  our dca_lbd_createsample.do for offshoring)


**For years 2009 through 2011 we can use the files as they are, just attaching firm-industry
 forvalues i = 2009(1)2011 {
cap !gunzip lbd_dca_`i'_firm.dta.gz
!gunzip lbd_dca_`i'_clean.dta.gz

use lbd_dca_`i'_clean.dta, clear


gen mu = 0
replace mu = 1 if substr(firmid,7,.)=="0000"

collapse (sum) emp pay (firstnm) cfn, by(firmid mu flag_us_mult flag_for_mult ifscode)

**For single-units, replace firmid==cfn (for lfttd)
replace firmid=cfn if mu==0

drop if firmid==""

**Some single-unit cfns that had a missing cfn
collapse (sum) emp pay (max) mu flag_us_mult flag_for_mult ifscode, by(firmid)

merge 1:1 firmid using firmnaics`i'.dta
drop if _m==2
drop _m

save lbd_dca_`i'_firm.dta, replace
!gzip lbd_dca_`i'_clean.dta
}


**For year 2012, best we can do is attach mult flags, etc, from 2011 (we want to keep these constant anyway)
cap !gunzip lbd_dca_2012_firm.dta.gz
!gunzip $BFPNDATA/lbd2012c.dta.gz
use $BFPNDATA/lbd2012c.dta, clear

keep firmid cfn emp pay mu
collapse (sum) emp pay (firstnm) cfn, by(firmid mu)

**For single-units, replace firmid==cfn (for lfttd)
replace firmid=cfn if mu==0

drop if firmid==""

**Some single-unit cfns that had a missing cfn
collapse (sum) emp pay (max) mu, by(firmid)

gen alpha = substr(firmid,1,6)
gen year = 2012

**We will be using multinational identifiers from 2011
merge 1:1 firmid using lbd_dca_2011_firm.dta
drop if _m==2
drop _m

replace flag_us_mult = 0 if flag_us_mult==.
replace flag_for_mult = 0 if flag_for_mult==.
replace ifscode = 111 if ifscode==.
drop naics_code

**We'd like to keep the firmnaics constant into 2012 if possible
merge 1:1 firmid using firmnaics2011.dta
drop if _m==2

**For those that didn't match (new in 2012), now use 2012
preserve
keep if _m==1
drop _m
merge 1:1 firmid using firmnaics2012.dta
drop if _m==2
drop _m
save temp_2012_ind.dta, replace
restore

drop if _m==1
append using temp_2012_ind.dta
drop _m

save lbd_dca_2012_firm.dta, replace
erase temp_2012_ind.dta


!gzip $BFPNDATA/lbd2012c.dta



**--------------------------------------------------------------------------
**Step 3: Bring in CMF-Products File and create Int/Final Distinctions
**--------------------------------------------------------------------------


**take survu_id from 2007 and attach industry
foreach num of numlist 2007 {
	!gunzip lbd`num'c.dta.gz
	use lbd`num'c.dta, clear
	rename cfn survu_id
	keep survu_id lbdnum bestnaics
	bys survu_id: gen dup = _N
	drop if dup>1
	drop dup
	sort survu_id
	save lbd`num'temp.dta, replace

	use cmf_prod_naics_`num'.dta, clear
	sort survu_id
	merge survu_id using lbd`num'temp.dta
	tab _m
	keep if _m==3
	drop _m
	rename bestnaics naics_code
	save cmf_2007_prod_finalgoods.dta, replace
	!gzip /lbd`num'c.dta
	erase lbd`num'temp.dta
}




**Step 3.2 Generate 2007 Prod Thresholds for 8-Digit Naics, and 7-Digit, 6 Digit

**Base is 8-Digit Industry
use cmf_2007_prod_finalgoods.dta, clear
collapse (sum) prod, by(naics_code baseroot)
sort naics_code
bys naics_code: egen prod_tot = sum(prod)
gen ratio = prod/prod_tot
*Label as Final Good if one product occupies more than 10% of Total
gen final_pot = 0
replace final_pot = 1 if ratio>0.10
bys naics_code: egen num_final_pot = sum(final_pot)
sort naics_code
save cmf_2007_prod_finalgoods_temp.dta, replace

**Collapse by 6-Digit, 5-Digit, 4-Digit Industry, in the event we can't find a 
** match at the  8-digit level
forvalues j = 6(-1)4 {
	use cmf_2007_prod_finalgoods.dta, clear
	gen naics_code`j' = substr(naics_code,1,`j')
	collapse (sum) prod, by(naics_code`j' baseroot)
	bys naics_code`j': egen prod_tot = sum(prod)
	gen ratio = prod/prod_tot

	**Label as Final Good if one product occupies more than 10% of Total
	gen final_pot = 0
	replace final_pot = 1 if ratio>0.10
	bys naics_code`j': egen num_final_pot = sum(final_pot)
	sort naics_code`j'
	save cmf_2007_prod_finalgoods_temp`j'.dta, replace
}


**Step 3.3 Year 2009-2012
forvalues yr = 2009(1)2012 {

	cap !gunzip dca_lbd_`i'_intprod.dta.gz
	use lbd_dca_`yr'_firm.dta, clear
	keep naics_code
	duplicates drop
	
	drop if substr(naics_code,1,1)~="3"
	sort naics_code
	merge 1:m naics_code using cmf_2007_prod_finalgoods_temp.dta
	tab _m
	
	**If can't find an industry at 8-digit level in CMF, find one at 
	** Less disaggregated level.
	preserve
	keep if _m==3
	drop _m
	save temp_8digit.dta, replace
	restore
	keep if _m==1
	drop _m
	forvalues j = 6(-1)4 {
		keep naics_code
		gen naics_code`j' = substr(naics_code,1,`j')
		collapse (first) naics_code, by(naics_code`j')	
		sort naics_code`j'
		merge 1:m naics_code`j' using cmf_2007_prod_finalgoods_temp`j'.dta
		tab _m
		preserve
		keep if _m==3
		drop _m
		save temp_`j'digit.dta, replace
		restore
		keep if _m==1
		drop _m
		drop naics_code`j'
	}
	append using temp_4digit.dta
	append using temp_5digit.dta
	append using temp_6digit.dta
	append using temp_8digit.dta
	
	keep naics_code prod prod_tot ratio final_pot num_final_pot baseroot
	sort naics_code
	save ind_prod_temp.dta, replace

	use lbd_dca_`yr'_firm.dta, clear
	keep firmid naics_code
	drop if substr(naics_code,1,1)~="3"
	sort naics_code
	
	**Cartesian Product!!
	joinby naics_code using ind_prod_temp.dta
		
	sort firmid baseroot
	save dca_lbd_`yr'_intprod.dta, replace
	
}
erase temp_4digit.dta 
erase temp_5digit.dta
erase temp_6digit.dta
erase temp_8digit.dta
erase ind_prod_temp.dta
erase cmf_2007_prod_finalgoods_temp6.dta
erase cmf_2007_prod_finalgoods_temp5.dta
erase cmf_2007_prod_finalgoods_temp4.dta



**--------------------------------------------------------------------------
**Step 4: Bring in CMF-Products File and create Int/Final Distinctions
**--------------------------------------------------------------------------

**Step 4.1 Prep LFTTD Data
forvalues i = 2009(1)2012 {
	foreach ddd in exp imp {
		!gunzip `ddd'_mon_`i'.dta.gz
		use `ddd'_mon_`i'.dta, clear
		rename naics baseroot
	
		replace nonrelvalue = 0 if nonrelvalue==.
		replace relvalue = 0 if relvalue==.
		
		gen jpn = 0
		replace jpn = 1 if country=="xxxx"
		gen na = 0
		replace na = 1 if country=="xxxx" | country=="xxxx"
		gen njpn = 0
		replace njpn = 1 if country~="xxxx"
	
		preserve
		keep if jpn == 1
		drop jpn
		collapse (sum) relvalue nonrelvalue, by(firmid baseroot month)
		rename relvalue j`ddd'_rel
		rename nonrelvalue j`ddd'_nrel
		save j`ddd'_temp.dta, replace
		restore
	
		preserve
		drop if jpn == 1
		collapse (sum) relvalue nonrelvalue, by(firmid baseroot month)
		rename relvalue nj`ddd'_rel
		rename nonrelvalue nj`ddd'_nrel
		save nj`ddd'_temp.dta, replace
		restore
	
		keep if na == 1
		collapse (sum) relvalue nonrelvalue, by(firmid baseroot month)
		rename relvalue na`ddd'_rel
		rename nonrelvalue na`ddd'_nrel
		save na`ddd'_temp.dta, replace
	
		merge 1:1 firmid baseroot month using nj`ddd'_temp.dta
		drop _m
		merge 1:1 firmid baseroot month using j`ddd'_temp.dta
		drop _m
	
		save `ddd'_monthly_`i'_temp.dta, replace
		keep firmid baseroot
		duplicates drop
		save `ddd'_`i'_temp.dta, replace
	}
}
	



**Step 4.2: Merge Together and Collapse by Type
forvalues i = 2009(1)2012 {
	foreach ddd in exp imp {
		use dca_lbd_`i'_intprod.dta, clear
		merge m:1 firmid baseroot using `ddd'_`i'_temp
		tab _m
		drop if _m==1
		drop _m
		replace final_pot = 0 if final_pot==.
		replace ratio = 0 if ratio==.
		
		merge 1:m firmid baseroot using `ddd'_monthly_`i'_temp.dta
		

		collapse (sum) na`ddd'_nrel na`ddd'_rel j`ddd'_nrel j`ddd'_rel nj`ddd'_nrel nj`ddd'_rel  , by(firmid final_pot month)
		reshape wide na`ddd'_rel na`ddd'_nrel j`ddd'_rel j`ddd'_nrel nj`ddd'_rel nj`ddd'_nrel, i(firmid month) j(final_pot)
		
		foreach vname in na`ddd'_rel na`ddd'_nrel j`ddd'_rel j`ddd'_nrel nj`ddd'_rel nj`ddd'_nrel {
			rename `vname'0 `vname'_int
			rename `vname'1 `vname'_fin
		}
		
		save mon_`ddd'_`i'_temp2.dta, replace
	}
}



**Step 4.3 Clean up Unnecessary Files
foreach ddd in exp imp {
	forvalues i = 2009(1)2012 {
		erase `ddd'_monthly_`i'_temp.dta
		erase `ddd'_`i'_temp.dta
		!gzip `ddd'_mon_`i'.dta
		!gzip dca_lbd_`i'_intprod.dta
	}
	erase j`ddd'_temp.dta
	erase nj`ddd'_temp.dta
	erase na`ddd'_temp.dta	
}



**--------------------------------------------------------------------------
**Step 5: Saturate, Create Other variables
**--------------------------------------------------------------------------


**4.1 Saturate the Monthly Panel
forvalues j = 2009(1)2012 {
	use mon_exp_`j'_temp2.dta, clear
	keep firmid
	duplicates drop
	save replicate_`j'.dta, replace
	gen month = 1
	compress
	forvalues i = 2(1)12 {
		append using replicate_`j'.dta
		replace month= `i' if month==.
	}
	sort firmid month
	merge firmid month using mon_exp_`j'_temp2.dta
	drop _m
	save mon_exp_`j'_temp3.dta, replace
	erase mon_exp_`j'_temp2.dta
	erase replicate_`j'.dta

	use mon_imp_`j'_temp2.dta, clear
	keep firmid
	duplicates drop
	save replicate_`j'.dta, replace
	gen month = 1
	compress
	forvalues i = 2(1)12 {
		append using replicate_`j'.dta
		replace month= `i' if month==.
	}
	sort firmid month
	merge firmid month using mon_imp_`j'_temp2.dta
	drop _m
	save mon_imp_`j'_temp3.dta, replace
	erase mon_imp_`j'_temp2.dta
	erase replicate_`j'.dta
}

**4.2 Generate extra variables (collapsing int/fin rel/nrel distinctions)
forvalues j = 2009(1)2012 {
	use mon_exp_`j'_temp3.dta, clear 
	
	foreach var of varlist jexp_rel_int jexp_rel_fin jexp_nrel_int jexp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	foreach var of varlist njexp_rel_int njexp_rel_fin njexp_nrel_int njexp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	foreach var of varlist naexp_rel_int naexp_rel_fin naexp_nrel_int naexp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	
	gen naexp = naexp_rel_int + naexp_nrel_int + naexp_rel_fin + naexp_nrel_fin
	gen naexp_rel = naexp_rel_int + naexp_rel_fin
	gen naexp_nrel = naexp_nrel_int + naexp_nrel_fin
	gen naexp_int = naexp_rel_int + naexp_nrel_int
	gen naexp_fin = naexp_rel_fin + naexp_nrel_fin
	
	gen jexp = jexp_rel_int + jexp_nrel_int + jexp_rel_fin + jexp_nrel_fin
	gen jexp_rel = jexp_rel_int + jexp_rel_fin
	gen jexp_nrel = jexp_nrel_int + jexp_nrel_fin
	gen jexp_int = jexp_rel_int + jexp_nrel_int
	gen jexp_fin = jexp_rel_fin + jexp_nrel_fin
	
	gen njexp = njexp_rel_int + njexp_nrel_int + njexp_rel_fin + njexp_nrel_fin
	gen njexp_rel = njexp_rel_int + njexp_rel_fin
	gen njexp_nrel = njexp_nrel_int + njexp_nrel_fin
	gen njexp_int = njexp_rel_int + njexp_nrel_int
	gen njexp_fin = njexp_rel_fin + njexp_nrel_fin
	
	
	
	
	save mon_exp_`j'_temp4.dta, replace 
	erase mon_exp_`j'_temp3.dta
}

forvalues j = 2009(1)2012 {
	use mon_imp_`j'_temp3.dta, clear 
	
	
	foreach var of varlist jimp_rel_int jimp_nrel_int jimp_rel_fin jimp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	foreach var of varlist njimp_rel_int njimp_nrel_int njimp_rel_fin njimp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	foreach var of varlist naimp_rel_fin naimp_rel_int naimp_nrel_fin naimp_nrel_int {
		replace `var' = 0 if `var'==.
	}
		
	gen njimp = njimp_rel_int + njimp_nrel_int + njimp_rel_fin + njimp_nrel_fin
	gen njimp_rel = njimp_rel_int + njimp_rel_fin
	gen njimp_nrel = njimp_nrel_int + njimp_nrel_fin
	gen njimp_int = njimp_rel_int + njimp_nrel_int
	gen njimp_fin = njimp_rel_fin + njimp_nrel_fin
	gen jimp = jimp_rel_int + jimp_nrel_int + jimp_rel_fin + jimp_nrel_fin
	gen jimp_rel = jimp_rel_int + jimp_rel_fin
	gen jimp_nrel = jimp_nrel_int + jimp_nrel_fin
	gen jimp_int = jimp_rel_int + jimp_nrel_int
	gen jimp_fin = jimp_rel_fin + jimp_nrel_fin
	gen naimp_int = naimp_rel_int + naimp_nrel_int
	gen naimp_fin = naimp_rel_fin + naimp_nrel_fin
	
	
	
	save mon_imp_`j'_temp4.dta, replace 
	erase mon_imp_`j'_temp3.dta
}


forvalues j = 2009(1)2012 {
	use mon_exp_`j'_temp4.dta, clear
	gen year = `j'
	sort firmid month
	save exp_firm`j'monthly_clean.dta, replace
	erase mon_exp_`j'_temp4.dta
	
	use mon_imp_`j'_temp4.dta, clear
	gen year = `j'
	sort firmid month
	save imp_firm`j'monthly_clean.dta, replace
	erase mon_imp_`j'_temp4.dta
}

**--------------------------------------------------------------------------
**Step 5: Create Dataset of Monthly Exports, Imports and Firm-Ownership
**--------------------------------------------------------------------------

**-----------------------------------------------------
**5.1 Prep DCA_LBD Data
forvalues j = 2009(1)2012 {
	use lbd_dca_`j'_firm.dta, clear

	*Reduce Variables
	*drop flag_for_mult
	gen japan = 0
	replace japan = 1 if ifscode==158
	
	compress
	
	gen naics2 = substr(naics_code,1,2)
	keep if naics2=="xx" | naics2=="xx" | naics2=="xx"
	drop naics2

	save dca_lbd_`j'_temp.dta, replace
	**Create Monthly Copies
	gen month = 1
	compress
	forvalues i = 2(1)12 {
		append using dca_lbd_`j'_temp.dta
		replace month= `i' if month==.
	}
	sort firmid month
	save dca_lbd_`j'_month.dta, replace

	**5.2 Merge Together Exports and Imports Files
	use imp_firm`j'monthly_clean.dta, clear
	merge firmid month using exp_firm`j'monthly_clean.dta
	drop _m

	**5.3 Now Merge Together With Monthly Version of DCA_LBD
	compress
	sort firmid month
	merge firmid month using dca_lbd_`j'_month.dta
	tab _m	
	**_m==1 ==> Non-Manufacturing Firms that Trade
	**_m==2 ==> Manufacturing Firms that Don't Trade
	**_m==3 ==> Manufacturing Firms that Trade
	drop if _m==1
	drop _m
	capture destring year, replace
	replace year = `j'
	compress
	save DCA_LBD_LFTTD_`j'_month.dta, replace
	!gzip lbd_dca_`j'_firm.dta
}
**-----------------------------------------------------


use DCA_LBD_LFTTD_2009_month.dta
**5.4 Stack 2010 and 2011 (and 2009?)
append using DCA_LBD_LFTTD_2010_month.dta
append using DCA_LBD_LFTTD_2011_month.dta

**Only keep if the firm is present in 2009, 2010, and 2011
bys firmid: gen copies = _N
tab copies
drop if copies<xx
drop copies
compress

**Now add 2012?
append using DCA_LBD_LFTTD_2012_month.dta



**Fixes to Firm-Level Indicators
**For These: Enforce all indicators to be japanese, foreign mult

	qui replace japan = 1 if firmid=="`fname'"
	qui replace flag_us_mult=0 if firmid=="`fname'"
	qui replace flag_for_mult=1 if  firmid=="`fname'"
}

	qui replace japan = 1 if firmid=="`fname'"
	qui replace flag_us_mult=0 if firmid=="`fname'"
	qui replace flag_for_mult=1 if firmid=="`fname'"
}


	qui replace japan = 1 if firmid=="`fname'"
	qui replace flag_us_mult=0 if firmid=="`fname'"
	qui replace flag_for_mult=1 if firmid=="`fname'"
}

**For These: Enforce all indicators to be non-japanese

	qui replace japan=0 if firmid=="`fname'"
}

replace flag_for_mult=1 if firmid == 
replace flag_for_mult=0 if firmid==
replace flag_for_mult=0 if firmid==
replace flag_us_mult=1 if firmid==
replace flag_for_mult=0 if firmid==
replace flag_us_mult=1 if firmid==
replace flag_for_mult=0 if firmid==
replace flag_for_mult=0 if firmid==
replace flag_us_mult=1 if firmid==
replace flag_for_mult=0 if firmid==

**For the rest with disagreements: replace as non-jpn
bys firmid: egen minjpn = min(japan)
replace japan = 0 if minjpn~=japan
drop minjpn

sum jimp_int if firmid=="" & year==2011 & (month==1 | month==2)
replace jimp_int = r(mean) if firmid=="" & year==2011 & (month==1 | month==2)
sum jimp_int if firmid=="" & year==2009 & (month==5 | month==6 | month==7)
replace jimp_int = r(mean) if firmid=="" & year==2009 & (month==1 | month==2 | month==3 | month==4)

sum naexp if firmid=="" & year==2011 & (month==10 | month==11 | month==12)
replace naexp = r(mean) if firmid=="" & year==2011 & (month==10 | month==11 | month==12)



**5.8 Modify Variables and Save
foreach var of varlist naimp_int jimp njimp jimp_int njimp_int jimp_fin njimp_fin jimp_rel njimp_rel jimp_nrel njimp_nrel {
	replace `var' = 0 if `var'==.
	
}

foreach var of varlist naexp_nrel_int naexp_nrel_fin jimp_rel_int jimp_nrel_int  {
	replace `var' = 0 if `var'==.
}

foreach var of varlist naexp naexp_rel naexp_rel_int naexp_rel_fin naexp_int naexp_nrel naexp_fin {
	replace `var' = 0 if `var'==.
}

foreach var of varlist njexp njexp_rel njexp_int njexp_nrel njexp_fin {
	replace `var' = 0 if `var'==.
}


cap !gunzip analysisdata_manuf.dta.gz
save analysisdata_manuf.dta, replace
!gzip analysisdata_manuf.dta

**--------------------------------------------------------------------------
**Step 5: Bring in Uniworld Data, do some checks
**--------------------------------------------------------------------------

*gen firmid = alpha
*gen lenfirmid = length(firmid)
*replace firmid = firmid + "0000" if lenfirmid==6



sort firmid
merge firmid using DFFOUS_2008_2011_ids.dta
tab _m
drop if _m==2
drop _m


**Uniworld Sample Included
replace japanU2008 = 0 if japan==0 & flag_for_mult==1
replace japanU2011 = 0 if japan==0 & flag_for_mult==1
gen japan_alt = japan
gen flag_for_mult_alt = flag_for_mult
replace japan = 1 if (japanU2008==1 & japanU2011==1) & flag_us_mult~=1
replace flag_for_mult = 1 if (flag_for_multU2011==1 & flag_for_multU2008==1)

**1.1 Ensure that the multinational flags are consistent
bys firmid: egen maxflagfor = max(flag_for_mult)
by firmid: egen maxflagus = max(flag_us_mult)
by firmid: egen maxflagjpn = max(japan)

replace flag_for_mult = maxflagfor
replace flag_us_mult = maxflagus
replace japan = maxflagjpn
drop maxflagjpn maxflagus maxflagfor

**Make sure Uniworld didn't screw anything up
replace japan = 0 if flag_us_mult==1
**For These: Enforce all indicators to be non-japanese

	replace japan=0 if firmid=="`fname'"
}
replace flag_for_mult=0 if firmid==""
replace flag_for_mult=0 if firmid==""

bys firmid year: egen sumjimp_int_rel = sum(jimp_rel_int)
bys firmid year: egen sumjimp_int_nrel = sum(jimp_nrel_int)
gen relshareyear = sumjimp_int_rel / (sumjimp_int_rel + sumjimp_int_nrel)
replace relshareyear = . if year~=2010
bys firmid: egen relshareyear2 = min(relshareyear)
drop relshareyear
rename relshareyear2 relshareyear
replace japan = 0 if japan==1 & japan_alt==0 & relshareyear<0.xx
drop relshareyear sumjimp_int_rel sumjimp_int_nrel

bys firmid year: egen sumimp_int_rel = sum(jimp_rel_int+njimp_rel_int)
bys firmid year: egen sumimp_int_nrel = sum(jimp_nrel_int+njimp_nrel_int)
gen trelshareyear = sumimp_int_rel / (sumimp_int_rel + sumimp_int_nrel)
replace trelshareyear = . if year~=2010
bys firmid: egen trelshareyear2 = min(trelshareyear)
drop trelshareyear
rename trelshareyear2 trelshareyear
drop if trelshare==0 & flag_for_mult==1 & flag_for_mult_alt==0
drop trelshareyear sumimp_int_rel sumimp_int_nrel

bys firmid year: egen sumjimp_int = sum(jimp_int)
replace sumjimp_int = . if year~=2010
bys firmid: egen sumjimp_int2 = min(sumjimp_int)
drop sumjimp_int
rename sumjimp_int2 sumjimp_int
replace japan = 0 if japan==1 & japan_alt==0 & sumjimp_int==0

cap !gunzip analysisdata_manuf.dta.gz
save analysisdata_manufU.dta, replace
!gzip analysisdata_manufU.dta

erase dca_lbd_2009_temp.dta
erase dca_lbd_2010_temp.dta
erase dca_lbd_2011_temp.dta
erase dca_lbd_2012_temp.dta
erase dca_lbd_2009_month.dta
erase dca_lbd_2010_month.dta
erase dca_lbd_2011_month.dta
erase dca_lbd_2012_month.dta
erase DCA_LBD_LFTTD_2009_month.dta
erase DCA_LBD_LFTTD_2010_month.dta
erase DCA_LBD_LFTTD_2011_month.dta
erase DCA_LBD_LFTTD_2012_month.dta
forvalues i = 2009(1)2012 {
	capture erase imp_firm`i'monthly_clean.dta
	capture erase exp_firm`i'monthly_clean.dta
}

